package com.meishij.fragment.finddetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.meishij.R;
import com.meishij.adapter.FindWenZhangViewPagerAdapter;
import com.meishij.entry.FindWenZhangEntry;
import com.meishij.server.FindServer;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 阿飞 on 2016/10/14.
 */

public class FindWenZhangActivity extends AppCompatActivity implements View.OnClickListener {

    private Retrofit retrofit;

    private View currentView;
    private static final String TAG = FindWenZhangActivity.class.getSimpleName();
    private TabLayout tabWenZheng;
    private ViewPager vpWenZhang;
    private FindWenZhangViewPagerAdapter vpWenZhangAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_detail_wenzhang);
        initView();
        initRetrofit();
        requestNetData();
    }

    private void initView() {
        View back = findViewById(R.id.iv_find_detail_header_back);
        back.setOnClickListener(this);

        TextView title = (TextView) findViewById(R.id.tv_find_detail_header_title);
        title.setText("饮食新闻");

        tabWenZheng = (TabLayout) findViewById(R.id.tab_find_detail_wenZhang);
        vpWenZhang = (ViewPager) findViewById(R.id.vp_find_detail_wenZhang);
        vpWenZhangAdapter = new FindWenZhangViewPagerAdapter(getSupportFragmentManager(), null, null);
        vpWenZhang.setAdapter(vpWenZhangAdapter);
        tabWenZheng.setupWithViewPager(vpWenZhang);
    }

    private void initRetrofit() {
        retrofit = new Retrofit.Builder()
                .baseUrl(FindServer.BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    private void requestNetData() {
        FindServer findServer = retrofit.create(FindServer.class);

        Call<FindWenZhangEntry> call = findServer.getWenZhangResult(null, null, "android", "json");
        call.enqueue(new Callback<FindWenZhangEntry>() {
            @Override
            public void onResponse(Call<FindWenZhangEntry> call, Response<FindWenZhangEntry> response) {
                FindWenZhangEntry wenZhangEntry = response.body();

                List<FindWenZhangEntry.ClassesBean> classes = wenZhangEntry.getClasses();
                for (FindWenZhangEntry.ClassesBean classItem : classes) {
                    String cid = String.valueOf(classItem.getId());
                    String title = classItem.getTitle();

                    FindWenZhangItemFragment itemFragment = new FindWenZhangItemFragment();
                    Bundle argument = new Bundle();
                    argument.putString("cid", cid);
                    itemFragment.setArguments(argument);

                    vpWenZhangAdapter.add(itemFragment, title);
                }
            }

            @Override
            public void onFailure(Call<FindWenZhangEntry> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(FindWenZhangActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void changeCurrentIndex(View toView) {
        if (toView != currentView) {
            currentView.setSelected(false);
            currentView = toView;
            toView.setSelected(true);
            switch (toView.getId()) {
                case R.id.tv_find_detail_caidan_zuiXin:
//                    requestNetData("1");
                    break;
                case R.id.tv_find_detail_caidan_zuiRe:
//                    requestNetData("2");
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_find_detail_header_back:
                finish();
                break;
        }
    }
}
